import { IFeidaoAiMobile } from '@dfeidao/atom-mobile/interfaces';
import set from '@dfeidao/atom-mobile/local/set';
import nodejs from '@dfeidao/atom-mobile/msg/nodejs';
import get_param from '@dfeidao/atom-mobile/nav/get-param';
import render from '@dfeidao/atom-mobile/render/render';
import { file_url, spaceid } from '../atom/config';
import whoami_getparams from '../atom/whoami_getparams';

export default async function a001(fd: IFeidaoAiMobile) {

	const users = await whoami_getparams(fd) as { userinfo: object, params: { teacher_name: string, teacher_no: string } };
	// console.log(users, '--------users--------');

	const user_whoami = users.userinfo as { productid: string, usercode: string, user_name: string };
	const productid = user_whoami.productid;
	set(fd, "productid", productid);// U0000026

	const teacher_name = users.params.teacher_name;
	const teacher_no = users.params.teacher_no;
	set(fd, "teacher_no", teacher_no);
	set(fd, "teacher_name", teacher_name);

	const student_no = await get_param(fd, 'student_no');
	// const student_no = 'XS000075';
	const class_id = await get_param(fd, 'class_id');
	// const class_id = "BJ000086";
	set(fd, 'class_id', class_id);
	const time = new Date().getTime();

	const res = await nodejs<{ data: Array<{ check_in_status: string }>, data1: Array<{ class_name: string }>, data2: Array<{ headimg: string, student_name: string; class_hour: string }>, data3: Array<{}> }>('app-manual-check/s001', { productid, student_no, class_id, time });// 签到表(check_in_status) 班级学生(class_name) 学生(class_hour,headimg) 学生家长(phone)
	// console.log(res, '----res---');
	if (res.data.length > 0 && res.data[0].check_in_status === '1') {
		render(fd, { data: { d: "1" } });
	} else {
		if (res.data2.length > 0) {
			set(fd, "class_hour", res.data2[0].class_hour);
			res.data2.forEach((i) => {
				i.headimg = i.headimg ? file_url + '/getfile?productid=' + spaceid + '&id=' + i.headimg : '';
			});
			set(fd, "name", res.data2[0].student_name);
			set(fd, "class_name", res.data1[0].class_name);
		}
		render(fd, { data: { d: "0", data1: res.data1, data2: res.data2, data3: res.data3 } });
	}
}
